//
//  HomeHeaderView.swift
//  worldexchange
//
//  Created by xionghao on 2019/2/12.
//  Copyright © 2019 xionghao. All rights reserved.
//

import UIKit
import FSPagerView
import Kingfisher


protocol HomeHeaderViewDelegate {
    func onHeaderViewActionClick(type:Int)
}


class HomeHeaderView: UICollectionReusableView,FSPagerViewDelegate,FSPagerViewDataSource {
    
    @IBOutlet weak var iconMoney: UIImageView!
    @IBOutlet weak var shopWrapperView: UIView!
    @IBOutlet weak var shopNameLabel: UILabel!
    
    @IBOutlet weak var exchangeTipsLabel: UILabel!
    @IBOutlet weak var lastUpdateTimeLabel: UILabel!
    
    var delegate:HomeHeaderViewDelegate?
    
    func numberOfItems(in pagerView: FSPagerView) -> Int {
        return bannerItems.count
    }
    
    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
        let url = URL(string: IMAGE_HOST + bannerItems[index])
        cell.imageView?.kf.setImage(with: url!)
        cell.imageView?.contentMode = .scaleAspectFill
        cell.imageView?.clipsToBounds = true
        
        
        return cell
    }
    

    var bannerItems:[String] = [] {
        didSet {
            pageControl.numberOfPages = bannerItems.count
            pagerView.reloadData()
        }
    }
  
    
   var pagerView:FSPagerView!
   var pageControl: FSPageControl!
    
    func pagerViewWillEndDragging(_ pagerView: FSPagerView, targetIndex: Int) {
        self.pageControl.currentPage = targetIndex
    }
    
    func pagerViewDidEndScrollAnimation(_ pagerView: FSPagerView) {
        self.pageControl.currentPage = pagerView.currentIndex
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        setupScrollView()
    }
    
    func setupScrollView() {
        let screenWidth = UIScreen.main.bounds.width
        let frame = CGRect(x: 0, y: 0, width: screenWidth, height: 200)
        let scrollView =  FSPagerView(frame: frame)
        scrollView.itemSize = CGSize(width: screenWidth, height: 200)
        scrollView.delegate = self
        scrollView.dataSource = self
        scrollView.automaticSlidingInterval = 5
        
        scrollView.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "cell")
        pagerView = scrollView
        self.addSubview(pagerView)
        
        pageControl = FSPageControl(frame: CGRect(x: 0, y: 150, width: screenWidth , height: 50))
        pageControl.contentHorizontalAlignment  = .center
        pageControl.interitemSpacing = 8
        self.addSubview(pageControl)
        self.shopWrapperView.qmui_borderColor = UIColor.qmui_color(withHexString: "#231815")
        self.shopWrapperView.qmui_borderWidth = 0.5
        self.shopWrapperView.qmui_borderPosition = [.bottom,.top,.left,.left]
        setupEvent()
//        scrollView?.imageURLStringsGroup = self.bannerItems
//        self.bannerView = scrollView!
//        self.addSubview(self.bannerView)
//        self.bannerView.pageControlAliment = DDGBannerScrollViewPageContolAlimentCenter
//        self.bannerView.pageControlStyle = DDGBannerScrollViewPageControlHorizontal
       
        
    }
    
    func setupEvent(){
        let tap = UITapGestureRecognizer(target: self, action: #selector(onShopClick))
        self.shopWrapperView.addGestureRecognizer(tap)
        self.shopWrapperView.isUserInteractionEnabled = true
        
        let tap1 = UITapGestureRecognizer(target: self, action: #selector(onRefreshClick))
        self.iconMoney.addGestureRecognizer(tap1)
        self.iconMoney.isUserInteractionEnabled = true
        
    }
    
    @objc func onRefreshClick(){
        delegate?.onHeaderViewActionClick(type: 0)
    }
    
    @objc func onShopClick(){
        delegate?.onHeaderViewActionClick(type: 1)
    }
}
